package com.glsc.ngateway.common.base.repo.mysql.cmdb;

import com.glsc.ngateway.common.base.domain.mysql.cmdb.CmdbDiskStandard;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface CmdbDiskStandardRepository extends JpaRepository<CmdbDiskStandard, Integer>, JpaSpecificationExecutor<CmdbDiskStandard> {
    List<CmdbDiskStandard> findAllByIdIn(List<Long> id);

    @Query(value = "select m from CmdbDiskStandard m where m.isDelete=0 " +
            "and (:#{#paramSearchDto.diskStandardCode} is null OR m.diskStandardCode LIKE CONCAT('%', :#{#paramSearchDto.diskStandardCode},'%') ) " +
            "and (:#{#paramSearchDto.name} is null OR m.name LIKE CONCAT('%',:#{#paramSearchDto.name},'%') ) " +
            "and (:#{#paramSearchDto.brand} is null OR m.brand = :#{#paramSearchDto.brand}) " +
            "and (:#{#paramSearchDto.partNum} is null OR m.partNum LIKE CONCAT('%',:#{#paramSearchDto.partNum}, '%')) " +
            "and (:#{#paramSearchDto.diskType} is null OR m.diskType LIKE CONCAT('%', :#{#paramSearchDto.diskType}, '%')) " +
            "and (:#{#paramSearchDto.capacity} is null OR m.capacity = :#{#paramSearchDto.capacity}) " +
            "and (:#{#paramSearchDto.size} is null OR m.size = :#{#paramSearchDto.size}) ")
    Page<CmdbDiskStandard> searchByParam(Pageable pageable, CmdbDiskStandard paramSearchDto);

    @Transactional
    @Modifying
    @Query(value = "update t_cmdb_disk_standard set is_delete = :deleted where disk_standard_code in :codes ", nativeQuery = true)
    void updateIsDeleteByCodes(Integer deleted, List<String> codes);

    CmdbDiskStandard findOneByDiskStandardCode(String diskStandardCode);

    List<CmdbDiskStandard> findAllByNameAndIsDelete(String name, Integer noDelete);

    List<CmdbDiskStandard> findAllByPartNumAndIsDelete(String partNum, Integer noDelete);
}

